package cn.edu.lingnan.servlet;

import cn.edu.lingnan.pojo.Project;
import cn.edu.lingnan.pojo.ProjectAdult;
import cn.edu.lingnan.pojo.ScoreDTO;
import cn.edu.lingnan.pojo.User;
import cn.edu.lingnan.service.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


public class StudentLogin extends HttpServlet {
    userservice us=new userserviceimp();
    private ScoreService service=new ScoreServiceImp();
    private ProjectService projectService= new ProjectServiceImp();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println( "11111"+username + " " + password);

        User user = us.UserLogin(username, password);
        System.out.println("登录的用户信息为："+user);
        List<ScoreDTO> courses = service.getCoursesBySid(user.getId());
        List<ScoreDTO> scores = service.getScoresBySid(user.getId());
        List<ScoreDTO> noCoursesBySid = service.getNoCoursesBySid(user.getId());
        List<Project> noprojects = projectService.queryNoProjects(user.getName());
        List<ProjectAdult> projectAdults = projectService.studentqueryaduitProject(user.getId());
        List<Project>  enteredProject = projectService.querymyEnterProject(user.getName());
        List<Project>  AdultProjects =      projectService.StudentQueryCreateAdultProject(user.getName());
        List<Project> MyProjects =    projectService.queryMyProjects(user.getName());
        List<Project> rejectedProject = projectService.QueryRejectedProject(user.getName());
        if (user != null && user.getIdentity() != 0) {
            req.getSession().setAttribute("user", user);
            req.getSession().setAttribute("courses", courses);
            req.getSession().setAttribute("scores", scores);
            req.getSession().setAttribute("noCoursesBySid", noCoursesBySid);
            req.getSession().setAttribute("noprojects", noprojects);
            req.getSession().setAttribute("projectAdults", projectAdults);
            req.getSession().setAttribute("enteredProject", enteredProject);
            req.getSession().setAttribute("AdultProjects", AdultProjects);
            req.getSession().setAttribute("MyProjects", MyProjects);
            req.getSession().setAttribute("rejectedProject", rejectedProject);
            for (Project p : enteredProject) {
                System.out.println(p);
            }
//            req.getSession().setAttribute("message", "登录成功！");
            resp.sendRedirect(req.getContextPath() + "/student/main.jsp");
        } else if (user == null) {
            req.setAttribute("error", "登录失败，请检查用户名或密码");
            req.getRequestDispatcher("/error.html").forward(req, resp);
        } else {

            req.getSession().setAttribute("message", "该用户已冻结，请联系管理员");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }
}
